package com.ruoyi.factory.qylock.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.factory.qylock.domain.EreceiptHead;
import com.ruoyi.factory.qylock.mapper.EreceiptHeadMapper;
import com.ruoyi.factory.qylock.service.QysEreceiptService;
import com.ruoyi.factory.qylock.service.QysHandleService;
import com.ruoyi.factory.qylock.utils.OkHttpUtils;
import com.ruoyi.factory.qylock.utils.UicodeBackslashU;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName QysEreceiptService
 * @Description: TODO
 * @Author LiuJiaji
 * @Date 2024/5/16 09:58
 **/
@Service
public class QysEreceiptServiceImpl implements QysEreceiptService {
    protected final Logger logger = LoggerFactory.getLogger(QysEreceiptServiceImpl.class);

    @Autowired
    private QysHandleService qysHandleService;

    @Autowired
    private EreceiptHeadMapper ereceiptHeadMapper;


    public AjaxResult upStatus(Map<String, String> request){
        //根据文件id修改状态
        //解析数据
        JSONObject jsonObject = JSONObject.fromObject(request);
        logger.info("电子收据契约锁回调参数"+jsonObject.toString());
        String status = jsonObject.optString("status");

        //判断状态
        if(StringUtils.isNotEmpty(status)){
            String fileid = jsonObject.optString("contractId");
            EreceiptHead ephd = new EreceiptHead();
            ephd.setContractId(fileid);
            List<EreceiptHead> ereceiptHeads = ereceiptHeadMapper.selectEreceiptHeadList(ephd);
            if(!ereceiptHeads.isEmpty()){
                if("COMPLETE".equals(status)){
                    ephd.setEbillstate("已签署");
                }else if("RECALLED".equals(status)){
                    ephd.setEbillstate("已失效");
                }
                ereceiptHeadMapper.updateEreceiptHead(ephd);
            }
        }
        return new AjaxResult(200,"成功");
    }
    public AjaxResult pushtoqys(String request){

        JSONObject jsonObject3 = JSONObject.fromObject(request);
        //数据处理
        JSONObject jsonObject2 =handledata(jsonObject3);
        String s = jsonObject2.toString();
        System.out.println(s);

        //封装数据
        String url = "/contract/createbycategory";
        //请求契约锁
        logger.info("请求契约锁发起电子签约url: " + url);
        logger.info("电子收据请求契约锁参数 : "+jsonObject2.toString());
        String s2 = new Date().toString();
        logger.info("请求契约锁发起电子签约时间 : "+s2);
        String res2 = OkHttpUtils.okHttpPostqys(url, jsonObject2.toString());
        String s3 = new Date().toString();
        logger.info("请求契约锁发起签章返回结果时间 : "+s3);
        res2 = UicodeBackslashU.unicodeToCn(res2);
        logger.info("电子收据请求契约锁发起电子签约返参: " + res2);
        //处理返回结果
        JSONObject res = JSONObject.fromObject(res2);
        String rcode1 = res.optString("code");
        String message = res.optString("message");
        if("0".equals(rcode1) && "SUCCESS".equals(message)){
            //成功 更新状态
            String contractId = res.optString("contractId");
            String code = jsonObject3.optString("code");
            EreceiptHead pere = new EreceiptHead();
            pere.setCode(code);
            List<EreceiptHead> ereceiptHeads = ereceiptHeadMapper.selectEreceiptHeadList(pere);
            EreceiptHead ereceiptHead = ereceiptHeads.get(0);
            ereceiptHead.setEbillstate("签署中");
            ereceiptHead.setContractId(contractId);
            int i = ereceiptHeadMapper.updateEreceiptHeadstate(ereceiptHead);
        }
        return new AjaxResult(200,"请求成功",res);

    }


    public JSONObject handledata(JSONObject jsonObject3){

        JSONObject data = new JSONObject();

        //标题 电子收据-组织-编号
//        JSONObject jsonObject3 = JSONObject.fromObject(request);
        String code = jsonObject3.optString("code");
        EreceiptHead ereceiptHead = ereceiptHeadMapper.selectEreceiptHeadByCode(code);
        JSONObject jsonObject = JSONObject.fromObject(ereceiptHead);
        String kjperson = jsonObject.optString("kjperson");
        String groupname = jsonObject.optString("groupname");
        String subject = "电子收据-"+groupname+"-"+code;
        data.put("subject",subject);
        data.put("tenantName",groupname);
        data.put("sn",code);
        String billtype = jsonObject.optString("billtype");

        //流程和模板
        if("新乡市瑞丰新材料股份有限公司".equals(groupname)){
            //流程id
            JSONObject param = new JSONObject();
            if("C".equals(billtype)){
                param.put("billtype","DZSJ02");
            }else{
                param.put("billtype","DZSJ01");
            }
            param.put("pk_org","0001A110000000002TJA");
            AjaxResult ajaxResult = qysHandleService.normalTemplateInfo(param.toString());
            JSONArray data1 = (JSONArray)ajaxResult.get("data");
//            String categoryId = "";
            if (!data1.isEmpty()){
                JSONObject res = (JSONObject)data1.get(0);
                String id = res.optString("id");
                //模板
                if(StringUtils.isNotEmpty(id)){
                    data.put("categoryId",id);
                    Map<String, String> stringStringMap = qysHandleService.getqysfileconfig2(id);

                    String s = "";
                    if("A".equals(billtype)){
                        s = stringStringMap.get("01");
                    }else if("B".equals(billtype)){
                        s = stringStringMap.get("02");
                    }else if("C".equals(billtype)){
                        s = stringStringMap.get("03");
                    }
                    if(StringUtils.isNotEmpty(s)){
                        JSONArray templatelist = new JSONArray();
                        templatelist.add(s);
                        data.put("templates",templatelist);
                    }
                }
            }
        }

        JSONObject signatorie = new JSONObject();
        signatorie.put("tenantType","COMPANY");
        signatorie.put("tenantName",groupname);
//        signatorie.put("serialNo","");
        signatorie.put("receiverName","18288888888");
        signatorie.put("contact","18288888888");
        JSONArray signatorielist = new JSONArray();
        signatorielist.add(signatorie);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        JSONArray valuelist = new JSONArray();
        JSONObject value1 = new JSONObject();
        value1.put("name","收据编号");
        value1.put("value",code);
        valuelist.add(value1);
        Date billdate1 = ereceiptHead.getBilldate();
        String billdate12 = sdf.format(billdate1);
        JSONObject value8 = new JSONObject();
        value8.put("name","开具日期");
        value8.put("value",billdate12);
        valuelist.add(value8);
        String ksname = jsonObject.optString("ksname");
        JSONObject value7 = new JSONObject();
        value7.put("name","客商名称");
        value7.put("value",ksname);
        valuelist.add(value7);
        String skway = jsonObject.optString("skway");
        JSONObject value2 = new JSONObject();
        value2.put("name","收款方式");
        value2.put("value",skway);
        valuelist.add(value2);
        String mondx = jsonObject.optString("mondx");
        JSONObject value3 = new JSONObject();
        value3.put("name","合计大写");
        value3.put("value",mondx);
        valuelist.add(value3);
        JSONObject value5 = new JSONObject();
        value5.put("name","开具人");
        value5.put("value",kjperson);
        valuelist.add(value5);
        String jbperson = jsonObject.optString("jbperson");
        JSONObject value6 = new JSONObject();
        value6.put("name","业务员");
        value6.put("value",jbperson);
        valuelist.add(value6);
        JSONArray jsonArray = jsonObject.optJSONArray("ereceiptBodyList");
        for (int i = 0; i < jsonArray.size(); i++) {
            int j = i+1;
            JSONObject jsonObject2 = jsonArray.optJSONObject(i);
            String iteam = jsonObject2.optString("iteam");
            if(StringUtils.isEmpty(iteam)){
                continue;
            }else if("null".equals(iteam)){
                continue;
            }
            JSONObject itemj = new JSONObject();
            itemj.put("name","项目"+j);
            itemj.put("value",iteam);
            valuelist.add(itemj);
            String remark = jsonObject2.optString("remark");
            if(StringUtils.isNotEmpty(remark)){
                JSONObject remarkj = new JSONObject();
                remarkj.put("name","备注"+j);
                remarkj.put("value",remark);
                valuelist.add(remarkj);
            }
            if("A".equals(billtype)){
                String imon = jsonObject2.optString("imon");
                JSONObject imonj = new JSONObject();
                imonj.put("name","金额"+j);
                imonj.put("value",imon);
                valuelist.add(imonj);
            }

        }

        String mon = jsonObject.optString("mon");
        JSONObject value4 = new JSONObject();
        value4.put("name","合计");
        value4.put("value",mon);
        valuelist.add(value4);
        if(!"A".equals(billtype)){
            Date skdate = ereceiptHead.getSkdate();
            String skdate2 = sdf.format(skdate);
            JSONObject value9 = new JSONObject();
            value9.put("name","收款日期");
            value9.put("value",skdate2);
            valuelist.add(value9);
        }


        data.put("signatories",signatorielist);
        data.put("documentParams",valuelist);

        return data;
    }



}
